package com.shaoshaohuo.app.ui;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.adapter.WithdrawWayAdapter;
import com.shaoshaohuo.app.entity.BaseEntity;
import com.shaoshaohuo.app.entity.SupportWithdrawWay;
import com.shaoshaohuo.app.entity.SupportWithdrawWayEntity;
import com.shaoshaohuo.app.net.HttpRequest.RequestListener;
import com.shaoshaohuo.app.net.RequestService;
import com.shaoshaohuo.app.view.TopbarView;

import java.util.List;

/**sssss
 * @author cao
 * aaaa  nihaoas
 */
public class AddPayAccountActivity extends BaseActivity implements OnClickListener {
    //asdasdasdfgxasdasdaasdfafafasdasdsd
    private TopbarView mTopbarView;
    //zdadsd
    private EditText et_name;
    private EditText et_phone;
    private EditText et_card_num;
    private EditText et_bank_name;
    private LinearLayout ll_card_type;
    private TextView tv_card_type;
    private Button b_commit;
    //aaaaa
    private List<SupportWithdrawWay> supportWayList;
    private AlertDialog cardTypeDialog;
    private SupportWithdrawWay selectWithdrawWay;
    //asdasds
    private String id, account, realname, mobile, subbranch;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_pay_account);
        initView();
        setUpView();
        getSupportWithdraw();
    }

    private void getSupportWithdraw() {
        RequestService.getInstance().supportWithdraw(this, SupportWithdrawWayEntity.class, new RequestListener() {

            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    SupportWithdrawWayEntity entity = (SupportWithdrawWayEntity) resultData;
                    supportWayList = entity.getData().getList();
                } else {
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
            }
        });
    }

    private void initView() {
        mTopbarView = (TopbarView) this.findViewById(R.id.topbar);

        et_name = (EditText) findViewById(R.id.et_name);
        et_phone = (EditText) findViewById(R.id.et_phone);
        et_card_num = (EditText) findViewById(R.id.et_card_num);
        et_bank_name = (EditText) findViewById(R.id.et_bank_name);
        ll_card_type = (LinearLayout) findViewById(R.id.ll_card_type);
        tv_card_type = (TextView) findViewById(R.id.tv_card_type);
        b_commit = (Button) findViewById(R.id.b_commit);

    }

    private void setUpView() {
        mTopbarView.setCenterText("添加银行卡");
        mTopbarView.setLeftView(true, true);
        ll_card_type.setOnClickListener(this);
        b_commit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_card_type:
                selectCardType();
                break;
            case R.id.b_commit:
                commit();
                break;
        }

    }

    private void commit() {
        realname = et_name.getText().toString().trim();
        if (TextUtils.isEmpty(realname)) {
            showToast("请输入持卡人姓名");
        }

        mobile = et_phone.getText().toString().trim();
        if (TextUtils.isEmpty(mobile)) {
            showToast("请输入持卡人电话");
        }

        if (selectWithdrawWay == null) {
            showToast("请选择提现方式");
            return;
        }
        id = selectWithdrawWay.getId();

        account = et_card_num.getText().toString().trim();
        if (TextUtils.isEmpty(account)) {
            showToast("请输入持卡人卡号");
            return;
        }

        subbranch = et_bank_name.getText().toString().trim();
        if (TextUtils.isEmpty(subbranch)) {
            showToast("请输入银行卡开户支行");
            return;
        }

        startLoadingDialog();

        RequestService.getInstance().addPayAccount(this, id, account, realname, mobile, subbranch, BaseEntity.class, new RequestListener() {
            @Override
            public void onSuccess(int requestCode, BaseEntity resultData) {
                dismissLoadingDialog();
                if (resultData.isOk()) {
                    setResult(Activity.RESULT_OK);
                    finish();
                } else {
                    showToast(resultData.getMsg());
                }
            }

            @Override
            public void onFailed(int requestCode, Exception error, String msg) {
                dismissLoadingDialog();
                showToast(R.string.please_check_network);
            }
        });


    }


    protected void selectCardType() {

        if (supportWayList == null || supportWayList.isEmpty()) {
            showToast("暂无支持的支付方式");
            return;
        }

        if (cardTypeDialog != null && cardTypeDialog.isShowing()) {
            return;
        }
        if (cardTypeDialog == null) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            WithdrawWayAdapter<SupportWithdrawWay> adapter = new WithdrawWayAdapter<SupportWithdrawWay>(this,
                    supportWayList, false);
            builder.setAdapter(adapter, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    selectWithdrawWay = supportWayList.get(which);
                    tv_card_type.setText(selectWithdrawWay.getTitle());
                }
            });
            cardTypeDialog = builder.create();
            cardTypeDialog.setTitle("选择卡类型");

        }
        cardTypeDialog.show();
    }

}
